% ~miro/libi/tex/psfigure.tex
%
% Macro definitions for including PostScript files in documents
%
% Written by Allan Heydon. Based on code by Brad White.
%
% Macros Defined:
%   \macpsfigure, \normalpsfigure
%   \macpsfigureScaled, \normalpsfigureScaled
%   \psfigure
%
% To Use in a LaTeX \figure:
% \begin{figure}[hbt] % options: h=here, b=bottom, t=top, p=page of floats
%    \macpsfigureScaled{\figplain}{5in}{3.5in}{0.6}{0.6}{example.ps}
%    \caption{A Centered 5 x 3.5 inch Picture Scaled by 6/10 in
%	      Both Directions.
%	      \label{fig:example-figure}}
% \end{figure}
%
% ---------------------------------------------------------------------------
%
% \macpsfigure{style}{width}{height}{file}
% \normalpsfigure{style}{width}{height}{file}
%
% where:
%  style	is one of:
%		   \figplain -- include the figure by itself,
%		   \figboxed -- include the figure with a box around it,
%		   \figomit  -- draw a box where the figure would go
%				and inside the box write the name, size,
%				and scaling of the figure,
%		   \figblank -- leave space for the figure, but draw
%				nothing, or
%		   \fignone  -- do not even leave space for the figure
%  width	is the width of the picture (a dimension)
%  height	is the height of the picture (a dimension)
%  file		is the name of the postscript file to include inside
%		the \special command
%
% Dimensions (for 'width' and 'height') in TeX must have units such as
% ``in'' for inches, ``cm'' for centimeter, ``pt'' for points. The
% units for the 'width' and 'height' dimensions need not be the same.
%
% These macros produce the PostScript figure in file 'file' centered
% on the page. The only difference between the ``mac'' and ``normal''
% macros is how the origin of the included PostScript file is
% considered to be situated in the picture. Use \macpsfigure for
% PostScript files generated by a Macintosh; use \normalpsfigure for
% PostScript files generated by programs like xfig(1).
%
% EXAMPLES:
%
% \macpsfigure{\figplain}{5in}{3in}{foo.ps}
%    Centers the picture ``foo.ps'' which is 5 inches across and 3
%    inches high.
%
% \normalpsfigure{\figomit}{3.2cm}{4.3in}{Figures/example-1.ps}
%    Centers (in a box) the picture in the file
%    ``Figures/example-1.ps'' which is 3.2 centimeters wide and
%    4.3 inches high.
%
% ---------------------------------------------------------------------------
%
\newcommand{\macpsfigure}[4]{%
\psfigure{#1}{#2}{#3}{1.0}{1.0}{#4}{\iftrue}}
%
\newcommand{\normalpsfigure}[4]{%
\psfigure{#1}{#2}{#3}{1.0}{1.0}{#4}{\iffalse}}
%
% ---------------------------------------------------------------------------
%
% \macpsfigureScaled{style}{width}{height}{width-scale}{height-scale}{file}
% \normalpsfigureScaled{style}{width}{height}{width-scale}{height-scale}{file}
%
% where:
%  style, width, height, file are as above
%  width-scale	is a factor representing how much the picture should
%		be scaled in the horizontal direction
%  height-scale is a factor representing how much the picture should
%		be scaled in the vertical direction
%
% These macros are like their couterparts \macpsfigure and
% \normalpsfigure, but they allow the user to specify that the picture
% should be scaled in either or both directions. The dimensions 'width'
% and 'height' should be the dimensions of the picture *before
% scaling*.
%
% ---------------------------------------------------------------------------
%
\newcommand{\macpsfigureScaled}[6]{%
\psfigure{#1}{#2}{#3}{#4}{#5}{#6}{\iftrue}}
%
\newcommand{\normalpsfigureScaled}[6]{%
\psfigure{#1}{#2}{#3}{#4}{#5}{#6}{\iffalse}}
%
% ---------------------------------------------------------------------------
%
% Define \figplain, \figboxed, \figblank, \figomit to be integers 0,1,2,3
% The order of these definitions if important to the operation of the
% \psfigure macro.
%
\def\figplain{0}
\def\figboxed{1}
\def\figomit{2}
\def\figblank{3}
\def\fignone{4}

%
% ---------------------------------------------------------------------------
%
% \psfigure{style}{x}{y}{x-scale}{y-scale}{file}{invert-origin-p}
%
% where:
%  style	is the type of figure to produce
%  x		is the width of the picture
%  y		is the height of the picture
%  x-scale	is the amount to scale the picture in the x direction
%  y-scale	is the amount to scale the picture in the y direction
%  file		is the name of the input file (extension required)
%  invert-origin-p	is either \iftrue or \iffalse depending on
%			whether or not the origin is at the top or
%			bottom of the picture, respectively.
%
% ---------------------------------------------------------------------------
%
\newcommand{\psfigure}[7]{{
    \ifnum #1=\fignone
%	Create a 0-width box; that is, the figure takes up no space.
        \mbox{}
    \else
	\countdef\width=127 \countdef\height=126
	\countdef\hoffset=125 \countdef\voffset=124
	\countdef\columnwidth=123
	\dimen255=#2 \dimen255=#4\dimen255 \width=\dimen255
	\dimen255=#3 \dimen255=#5\dimen255 \height=\dimen255
	\dimen255=\textwidth \columnwidth=\dimen255
% We *don't* want to center it, so give 0 hoffset
\hoffset=0
% \hoffset=-\columnwidth \advance\hoffset\width \divide\hoffset 2
% Old Macintosh origin was at upper-left corner.
%	\voffset=#7-\height\else 0\fi
% New Mac origin on lower edge but 125 LW dots from left margin.
% However, the picture's origin seems to be an additional 2.25 cm
% up from there, so we must make our origin artificially low.
% Note that the vertical scaling factor (#5) is brought into the
% calculation of this offset.
	\dimen255=2.25cm \dimen255=#5\dimen255 \voffset=#7\dimen255\else 0\fi
	\setlength{\unitlength}{1sp}
	\begin{picture}(\columnwidth,\height)(\hoffset,\voffset)
	  \ifcase #1
%	    Case: \figplain (0)
	      \put(0,0){\special{psfile=#6 hscale=#4 vscale=#5}} \or
%	    Case: \figboxed (1)
	      \put(0,0){\special{psfile=#6 hscale=#4 vscale=#5}}
	      \put(0,\voffset){\framebox(\width,\height){}} \or
%	    Case: \figomit (2)
	      \put(0,\voffset){\framebox(\width,\height){
		 \begin{tabular}{c}
		   #6#7 (Macintosh figure)\fi\\
		   width=#2, height=#3\\
		   hscale=#4, vscale=#5
		 \end{tabular}}} \or
%	    Case: \figblank (3)
%	      Do nothing, just creating the picture will leave the
%	      necessary space on the page.
%	    Default:
	      \else\typeout{Bad style for \psfigure}
	  \fi
	\end{picture}\hfill
    \fi
}}
